package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import data.Constant;
import data.DataQuerry;
import data.UserInfo;

/**
 * Servlet implementation class SystemPrivs
 */
@WebServlet("/systemPrivs")
public class SystemPrivs extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SystemPrivs() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String sysprivs = request.getParameter("system_privs");
		String gr_rv = request.getParameter("gr_rv");
		String adminopt = request.getParameter("adminopt");
		String name = request.getParameter("name");
		DataQuerry query = new DataQuerry();
		HttpSession session = request.getSession();
		UserInfo user = (UserInfo) session.getAttribute("user");

		if(user != null){
			query.connectData(user.getUsername(), user.getPwd());
			String result = query.assignSysPrivs(name, sysprivs, gr_rv, adminopt);
			request.setAttribute("Status", result);
			String url = "/SystemPrivs.jsp";
			RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher(url);
			dispatcher.forward(request, response);
			query.closeConnection();
		}else{
			response.sendRedirect("index.jsp");
		}
	}

}
